package com.cn.kabalifemerchants.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.base.MyApplication;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.config.OpenHelper;
import com.cn.kabalifemerchants.ui.login.LoginActivity;
import com.cn.kabalifemerchants.utils.SPUtil;
import com.cn.kabalifemerchants.view.dialogView.AgreementDialog;


/**
 * Created by 晓林 on 2019/10/16.
 * function:
 */
public class StartActivity extends AppCompatActivity {

    private final int MSG_WHAT_NEXT = 0x01;
    private final int DELAY_MILLIS = 2000;//延迟时间

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == MSG_WHAT_NEXT) {
                if (judgeIsAgreeAgreement()) {
                    toMainPage();
                    return;
                }
            }
        }
    };

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_start);
        initView();
    }

    public void initView() {
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // 首次进入，打开欢迎界面
        mHandler.sendEmptyMessageDelayed(MSG_WHAT_NEXT, DELAY_MILLIS);

    }

    private void toMainPage() {
        if (LoginHelper.isGuidePage()) {
            if (LoginHelper.isLogin()) {
                OpenHelper.startActivity(StartActivity.this, MainActivity.class);
                finish();
            } else {
                OpenHelper.startActivity(StartActivity.this, LoginActivity.class);
                finish();
            }
        } else {
            OpenHelper.startActivity(StartActivity.this, SplashActivity.class);
            finish();
        }
    }

    private boolean judgeIsAgreeAgreement() {
        if (!LoginHelper.isAgreeAgreement()) {
            AgreementDialog ageDialog = new AgreementDialog(this);
            ageDialog.getTv_btn_n().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ageDialog.dismiss();
                    finish();
                }
            });
            ageDialog.getTv_btn_y().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ageDialog.dismiss();
                    SPUtil.saveString(LoginHelper.IS_AGREE_AGREEMENT, "1");
                    MyApplication.getInstance().initAllSDK();

                    toMainPage();
                }
            });
            ageDialog.show();

            return false;
        }

        return true;
    }

    //    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_start);
//        new Thread() {
//            @Override
//            public void run() {
//                super.run();
//                try {
//                    sleep(1000);
////                    if(SPUtil.getInt(LoginHelper.FIRST_RUN)==1) {
////                        if (LoginHelper.isLogin()) {
////                            if (SPUtil.getInt(LoginHelper.ISUSER) == 1)
////                                $startActivityFinish(YMainActivity.class);
////                            else
////                                $startActivityFinish(SMainActivity.class);
////                        } else {
////                            $startActivityFinish(loginActivity.class);
////                        }
////                    }else{
////                        $startActivityFinish(SplashActivity.class);
////                    }
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        }.start();
//    }
}
